package com.kui.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kui.entity.Qikan;
import com.kui.entity.User;
import com.kui.entity.Wenxian;
import com.kui.service.QikanService;
import com.kui.service.WenxianService;

public class EditWenxianServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		
		if("member".equals(user.getRole())){
			response.sendRedirect("main.jspx");
			return;
		}
		
		String wid = request.getParameter("wid");
		WenxianService ws =new WenxianService();
		
		Wenxian w = ws.findById(wid);
		if(w == null){
			response.sendRedirect("wenxiancontrol.jspx");
			return;
		}
		
		QikanService qs = new QikanService();
		List<Qikan> qikanlist = qs.findAll();
		request.setAttribute("qikanlist", qikanlist);
		request.setAttribute("wenxian", w);
		
		
		request.getRequestDispatcher("WEB-INF/views/editwenxian.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String wid = request.getParameter("wid");
		WenxianService ws =new WenxianService();
		
		Wenxian w = ws.findById(wid);
		String qikan = request.getParameter("qikan");
		String time = request.getParameter("time");
		String bianhao = request.getParameter("bianhao");
		String type = request.getParameter("type");
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		String zhaiyao = request.getParameter("zhaiyao");
		String keyword = request.getParameter("keyword");
		
		w.setT_qikan_id(qikan);
		w.setTime(time);
		w.setId(qikan+"("+time+")"+bianhao);
		w.setT_type_id(type);
		w.setName(name);
		w.setAuthor(author);
		w.setDesc(zhaiyao);
		w.setKeyword(keyword);
		
		ws.update(w,wid);
		response.sendRedirect("wenxiancontrol.jspx");
		

	}

}
